package com.bw.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.core.creditStatus.DeleteStatus;
import com.bw.core.result.ResponseResult;
import com.bw.user.mapper.TbUserMapper;
import com.bw.user.pojo.TbOperatorAuth;
import com.bw.user.pojo.TbTaobaoAuth;
import com.bw.user.mapper.TbTaobaoAuthMapper;
import com.bw.user.pojo.TbUser;
import com.bw.user.service.TbTaobaoAuthService;
import com.bw.user.vo.TaoBaoAuthVo;
import com.bw.user.vo.TaoBaoJson;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 淘宝认证$信息表 服务实现类
 * </p>
 *
 * @author lv
 * @since 2021-03-23
 */
@Service
public class TbTaobaoAuthServiceImpl extends ServiceImpl<TbTaobaoAuthMapper, TbTaobaoAuth> implements TbTaobaoAuthService {

    @Autowired
    private TbTaobaoAuthMapper taobaoAuthMapper;
    @Autowired
    private TbUserMapper userMapper;

    @Override
    public ResponseResult taobao(TaoBaoAuthVo taoBaoAuthVo) {

        QueryWrapper<TbTaobaoAuth> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",taoBaoAuthVo.getUserid());
        List<TbTaobaoAuth> tbTaobaoAuths = taobaoAuthMapper.selectList(wrapper);

        //判断是否认证过淘宝
        if (CollUtil.isNotEmpty(tbTaobaoAuths)){
            return ResponseResult.error("已经认证过");
        }

        //调用第三方淘宝认证接口
        HashMap<String, Object> taoHashMap = new HashMap<>();
        taoHashMap.put("user_name",taoBaoAuthVo.getUserName());
        taoHashMap.put("taobao_password",taoBaoAuthVo.getTaobaoPassword());
        String post = HttpUtil.post("http://localhost:8080/api/common/taobaoAuth", taoHashMap);
        //处理第三方接口返回的json
        TaoBaoJson taoBaoJson = JSONObject.parseObject(post, TaoBaoJson.class);
        //根据id查询用户表获取用户信息
        Example example = new Example(TbUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",taoBaoAuthVo.getUserid());
        TbUser tUser = new TbUser();
        tUser.setId(taoBaoAuthVo.getUserid());
        TbUser tbUser = userMapper.selectById(tUser);
        if(tbUser==null){
            return ResponseResult.error(405,"用户不存在!");
        }
        //set需要的属性
        TbTaobaoAuth tbTaobaoAuth = new TbTaobaoAuth();
        tbTaobaoAuth.setCreateTime(new Date());
        tbTaobaoAuth.setIsDelete(DeleteStatus.NOT_DELETE);
        tbTaobaoAuth.setTaobaoAccount(taoBaoAuthVo.getTaobaoAccount());
        tbTaobaoAuth.setTaobaoData(JSONObject.toJSONString(taoBaoJson.getData().getRes_data()));
        tbTaobaoAuth.setTaobaoPassword(taoBaoAuthVo.getTaobaoPassword());
        tbTaobaoAuth.setUpdateTime(new Date());
        tbTaobaoAuth.setUserId(tbUser.getId());
        tbTaobaoAuth.setUserName(tbUser.getUsername());
        tbTaobaoAuth.setUserPhone(tbUser.getPhone());
        //添加
        taobaoAuthMapper.insert(tbTaobaoAuth);


        return ResponseResult.success();




    }
}
